package com.hao.tool.web.module.navigation.controller;

import com.hao.tool.common.enums.UseTypeEnum;
import com.hao.tool.web.module.navigation.entity.ModelTypeInfo;
import com.hao.tool.web.module.navigation.entity.NavigationInfo;
import com.hao.tool.web.module.navigation.service.ModelTypeService;
import com.hao.tool.web.module.navigation.service.NavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.List;

/**
 * 导航栏
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-21 21:46
 */
@Controller
@RequestMapping("/navigation")
public class NavigationController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private ModelTypeService modelTypeService;

    @GetMapping("")
    private ModelAndView toList() {
        ModelAndView mv = new ModelAndView("navigation/listBtn");
        mv.addObject("modelIds", 0);
        return mv;
    }

    @GetMapping("/i/{modelIds}")
    private ModelAndView toList2(@PathVariable String modelIds) {
        ModelAndView mv = new ModelAndView("navigation/listBtn");
        mv.addObject("modelIds", modelIds);
        return mv;
    }


    @GetMapping("/new")
    private ModelAndView toNew() {
        List<ModelTypeInfo> modelTypeInfos = modelTypeService.find(UseTypeEnum.SHOW.getCode(), null);
        List<ModelTypeInfo> existModelTypeInfos = new ArrayList<>();
        modelTypeInfos.forEach(modelTypeInfo -> {
            List<NavigationInfo> navigationInfoList = navigationService.findByModelId(UseTypeEnum.SHOW.getCode(), modelTypeInfo.getId(), null);

            if (navigationInfoList != null && !navigationInfoList.isEmpty()) {
                modelTypeInfo.setInfoList(navigationInfoList);
                existModelTypeInfos.add(modelTypeInfo);
            }
        });
        ModelAndView mv = new ModelAndView("navigation/listBtn2");
        mv.addObject("modelList", existModelTypeInfos);
        return mv;
    }

    @GetMapping("/listTable")
    private ModelAndView toTable() {
        List<ModelTypeInfo> infoList = modelTypeService.list();
        ModelAndView mv = new ModelAndView("navigation/listTable");
        mv.addObject("modelList", infoList);
        return mv;
    }

    @GetMapping("/to/{linkId}")
    private ModelAndView toLink(@PathVariable Long linkId) {
        NavigationInfo info = navigationService.getById(linkId);
        if (info != null) {
            ModelAndView mv = new ModelAndView(new RedirectView(info.getLinkUrl()));
            navigationService.incrementClickCount(linkId);
            return mv;
        } else {
            ModelAndView mv = new ModelAndView("common/404");
            return mv;
        }
    }
}
